﻿Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Public Class BangGia
    Private m_Ma, m_Ten, m_DiaChi As String
    Private m_MaHang, m_TenHang, m_DonViTinh As String
    Private m_GiaNhap As Decimal
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.BHBangGiaTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBangGia)
        Me.BHBangGiaDMHangTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBangGiaDMHang)
        Me.BHBangGiaDoiTuongApDungTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBangGiaDoiTuongApDung)
        Me.BangGiaNhapTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.BangGiaNhap)
        Me.TinhTrangPhieuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TinhTrangPhieu)
        Me.DoiTuongApDungTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DoiTuongApDung)
        Me.DoiTuongTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DoiTuong)
        Me.DMThoiGianTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMThoiGian)
        SplashScreenManager.CloseForm()
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.setMacDinhCotAn("ma", "BHBG" & System.Guid.NewGuid.ToString)
        Me.TinhtrangISearchLookupEdit.EditValue = "DXL"
        Me.NgaylapIDateEdit.EditValue = Today
        Me.MabanggiaITextEdit.EditValue = Me.QueriesTableAdapter1.getMaBangGia_BHBangGia("BHBG", CDate(TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
    End Sub
    Private Sub BangGia_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DMThoiGianISearchLookupEdit.EditValue = "TN"
        loadDuLieu()
    End Sub
    Public Overrides Sub setSubTruoc_Luu()
        Me.setSuaTruocKhiLuuTab1("mabanggia", Me.MabanggiaITextEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("ngaylap", Me.NgaylapIDateEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("ngaybatdau", NgaybatdauIDateEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("ngayketthuc", NgayketthucIDateEdit.EditValue)
    End Sub
    Public Sub New()
        Me.IGridViewPopUp1.setColumnCheck()
        Me.IGridViewPopUp2.setColumnCheck()
        InitializeComponent()
        Me.setSoTabThamChieu(0)
        Me.setSoTab(2)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.BHBangGia, BHBangGiaBindingSource, TableAdapterManager, BHBangGiaTableAdapter, MabanggiaITextEdit, TinhtrangISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.BHBangGiaDMHang, FKBHBangGiaDMHma3A4CA8FDBindingSource, BHBangGiaDMHangTableAdapter)
        Me.setTab2(Me.PCV_CLINICDataSet.BHBangGiaDoiTuongApDung, Me.FKBHBangGiaDoima3B40CD36BindingSource, Me.BHBangGiaDoiTuongApDungTableAdapter)
        Me.setPopUp1(Me.PCV_CLINICDataSet_Don.DoiTuong, DoiTuongBindingSource, DoiTuongTableAdapter)
        Me.setColumnsChangeTab1(New ArrayList From {"giavnd", "tilelai", "thuegtgt"})
        Me.setColumnValueTab1(Me.colEx1ChiTiet01, Me.colgiaban)
    End Sub
    Private Sub RepositoryItemISearchLookUpEdit_DMHangHoa_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_DMHangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
        IGridViewChiTiet01.SetFocusedRowCellValue("donvitinh", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Protected Overrides Sub BarButtonItemLamTuoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.BHBangGiaBindingSource.Position
        loadDuLieu()
        Me.BHBangGiaBindingSource.Position = position
    End Sub
    Private Sub BarButtonItemLuuPopUp1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuuPopUp1.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn lấy các đối tượng đã chọn vào danh sách không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.IGridViewPopUp1.setUpdateData()
            For index = 0 To Me.IGridViewPopUp1.RowCount - 1
                If CBool(Me.IGridViewPopUp1.GetRowCellValue(index, "Mark")) = True AndAlso getExistColumnValue(Me.IGridViewChiTiet02, "makhachhang", Me.IGridViewPopUp1.GetRowCellValue(index, "ma")) = False Then
                    Me.m_Ma = Me.IGridViewPopUp1.GetRowCellValue(index, "ma")
                    Me.m_Ten = Me.IGridViewPopUp1.GetRowCellValue(index, "ten")
                    Me.IGridViewChiTiet02.AddNewRow()
                End If
                Me.IGridViewPopUp1.SetRowCellValue(index, "Mark", False)
            Next
            For index1 = 0 To Me.IGridViewChiTiet02.RowCount - 1
                If IsDBNull(Me.IGridViewChiTiet02.GetRowCellValue(index1, "makhachhang")) Then
                    Me.IGridViewChiTiet02.DeleteRow(index1)
                End If
            Next
        End If
    End Sub
    Private Sub IGridViewChiTiet02_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet02.InitNewRow
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tenkhachhang", m_Ten)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("makhachhang", m_Ma)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab2(True)
    End Sub
    Private Sub IGridViewChiTiet01_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        'Me.IGridViewChiTiet01.SetFocusedRowCellValue("giaban", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab1(True)
    End Sub
    'xử lý khi click vô nút save của cửa sổ popup - lấy các giá trị đã check đưa vào lưới chi tiết
    Private Sub BarButtonItemLuuPopUp2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuuPopUp2.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn lấy các hàng hóa đã chọn vào danh sách không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.IGridViewPopUp2.setUpdateData()
            For index = 0 To Me.IGridViewPopUp2.RowCount - 1
                If CBool(Me.IGridViewPopUp2.GetRowCellValue(index, "Mark")) = True AndAlso getExistColumnValue(Me.IGridViewChiTiet01, "mahang", Me.IGridViewPopUp2.GetRowCellValue(index, "ma")) = False Then
                    Me.m_MaHang = Me.IGridViewPopUp2.GetRowCellValue(index, "ma")
                    Me.m_TenHang = Me.IGridViewPopUp2.GetRowCellValue(index, "ten")
                    Me.m_DonViTinh = Me.IGridViewPopUp2.GetRowCellValue(index, "donvitinh")
                    Me.m_GiaNhap = Me.IGridViewPopUp2.GetRowCellValue(index, "dongiavnd")
                    Me.IGridViewChiTiet01.SetFocusedRowCellValue("giavnd", Me.m_GiaNhap)
                    Me.IGridViewChiTiet01.SetFocusedRowCellValue("tilelai", Me.BarEditItemPopup1_1.EditValue)
                    Me.IGridViewChiTiet01.SetFocusedRowCellValue("thuegtgt", Me.BarEditItemPopup1_2.EditValue)
                    Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", Me.m_MaHang)
                    Me.IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", Me.m_TenHang)
                    Me.IGridViewChiTiet01.SetFocusedRowCellValue("donvitinh", Me.m_DonViTinh)
                    Me.IGridViewChiTiet01.AddNewRow()
                End If
                Me.IGridViewPopUp2.SetRowCellValue(index, "Mark", False)
            Next
            If IsDBNull(Me.IGridViewChiTiet01.GetFocusedRowCellValue("mahang")) Then
                Me.IGridViewChiTiet01.DeleteSelectedRows()
            End If
        End If
    End Sub
    'khi popup hiện ra, set lại text nó từ text của searchlookupedit khác (ví dụ như loại áp dụng là gì - set lên để cho user dễ nhớ)
    Private Sub RepositoryItemPopupContainerEdit_DoiTuongApDung_Popup(sender As Object, e As EventArgs) Handles RepositoryItemPopupContainerEdit_DoiTuongApDung.Popup
        Me.BarStaticItem1.Caption = Me.LoaiApDungISearchLookupEdit.Text
    End Sub
    Private Sub DMThoiGianISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles DMThoiGianISearchLookupEdit.EditValueChanged
        setDMThoiGian(Me.DMThoiGianISearchLookupEdit, TuNgayIDateEdit, DenNgayIDateEdit)
        Me.BarButtonItemLamTuoi.PerformClick()
    End Sub
End Class
